{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.utils import shuffle\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_set_list=('FrcSub','Math1', 'Math2')\n",
    "data_set_name=data_set_list[2]\n",
    "\n",
    "base_dir='../data/'\n",
    "if data_set_name == 'FrcSub':\n",
    "    read_dir=os.path.join(base_dir,'Math2015/FrcSub/')\n",
    "    save_dir=os.path.join(base_dir,'frcSub/')\n",
    "elif data_set_name == 'Math1':\n",
    "    read_dir=os.path.join(base_dir,'Math2015/Math1/')\n",
    "    save_dir=os.path.join(base_dir,'math1/')\n",
    "elif data_set_name == 'Math2':\n",
    "    read_dir=os.path.join(base_dir,'Math2015/Math2/')\n",
    "    save_dir=os.path.join(base_dir,'math2/')\n",
    "    \n",
    "X = np.loadtxt(os.path.join(read_dir, 'data.txt'))\n",
    "Q = np.loadtxt(os.path.join(read_dir, 'q.txt'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=pd.DataFrame(X,index=range(1,X.shape[0]+1),columns=range(1,X.shape[1]+1))\\\n",
    "        .reset_index().melt(id_vars='index')\n",
    "data.columns=['user_id','item_id','score']\n",
    "data=data.set_index('user_id')\n",
    "\n",
    "data=shuffle(data)\n",
    "data.to_csv(save_dir+'record.csv',index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "# test_ratio=0.2\n",
    "\n",
    "# data=shuffle(data)\n",
    "\n",
    "# boundary=int(len(data)*test_ratio)\n",
    "\n",
    "# test_data=data.iloc[:boundary,:]\n",
    "# train_data=data.iloc[boundary:,:]\n",
    "\n",
    "# test_data.to_csv(save_dir+'test.csv',index=True)\n",
    "# train_data.to_csv(save_dir+'train.csv',index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "#将习题保存为 json 格式\n",
    "\n",
    "# test_data_dict=test_data.loc[:,['user_id','item_id']]-1\n",
    "# test_data_dict.loc[:,'score']=test_data.loc[:,'score']\n",
    "# test_data_dict=list(test_data_dict.T.to_dict('dict').values())\n",
    "# test_data_dict=[{'user_id':int(x['user_id']),'item_id':int(x['item_id']),'score':x['score']} for x in test_data_dict]\n",
    "\n",
    "# train_data_dict=train_data.loc[:,['user_id','item_id']]-1\n",
    "# train_data_dict.loc[:,'score']=train_data.loc[:,'score']\n",
    "# train_data_dict=list(train_data_dict.T.to_dict('dict').values())\n",
    "# train_data_dict=[{'user_id':int(x['user_id']),'item_id':int(x['item_id']),'score':x['score']} for x in train_data_dict]\n",
    "\n",
    "# with open(save_dir+'test.json', 'w', encoding='utf8') as file:\n",
    "#     json.dump(test_data_dict, file, indent=4, ensure_ascii=False)\n",
    "# with open(save_dir+'train.json', 'w', encoding='utf8') as file:\n",
    "#     json.dump(train_data_dict, file, indent=4, ensure_ascii=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "def format_Q_matrix(Q):\n",
    "    problem_list=np.array(range(1,Q.shape[0]+1))\n",
    "    item_data=pd.DataFrame(columns=['item_id','knowledge_code'])\n",
    "    counter=0\n",
    "    for problem in range(1,len(problem_list)+1):\n",
    "        item_data.loc[counter,'item_id']=str(problem)\n",
    "        item_data.loc[counter,'knowledge_code']=list(np.argwhere(Q[problem-1,:]==1).reshape(-1)+1)\n",
    "        counter+=1\n",
    "    return item_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "item=format_Q_matrix(Q)\n",
    "\n",
    "item.to_csv(save_dir+'item.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt(save_dir+'q_m.csv', Q,  delimiter=',', fmt='%d')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "527a93331b4b1a8345148922acc34427fb7591433d63b66d32040b6fbbc6d593"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('pytorch': conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
